﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DataBase;

namespace YJKDataLookAfter
{
	public class DbDepartment : DbObject
	{
		public string Name
		{
			get
			{
				return this.mName;
			}
			set
			{
				this.mName = value;
			}
		}

		public string Description
		{
			get
			{
				return this.description;
			}
			set
			{
				this.description = value;
			}
		}

		public int ParentId
		{
			get
			{
				return this.parentId;
			}
			set
			{
				this.parentId = value;
			}
		}

		public DbDepartment(int id, string name, string desc, int parentId, IDatabase db)
		{
			this.mId = id;
			this.mName = name;
			this.ParentId = parentId;
			this.description = desc;
			this.mDatabase = db;
		}

		public DbDepartment(string name, string desc, int parentId)
		{
			this.mName = name;
			this.ParentId = parentId;
			this.description = desc;
		}

		public List<DbUser> GetAllUsers()
		{
			List<DbUser> list = new List<DbUser>();
			List<DbUser> result;
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("select UserId from tbDepartmentUserRelation where DepartmentId={0}", base.Id);
				DataTable dataTable = this.mDatabase.ExecQuery(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int id = Convert.ToInt32(dataTable.Rows[i]["UserId"]);
						list.Add(DbUser.GetFromDB(id, this.mDatabase));
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public List<DbDepartment> GetAllSubDepartment(bool isAll)
		{
			throw new Exception();
		}

		public bool ReName(string newName)
		{
			throw new Exception();
		}

		public void AddSubDepartment(DbDepartment department)
		{
			department.ParentId = base.Id;
			department.AddToDb(this.mDatabase);
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbDepartment (Name,Description,ParentId) VALUES ('{0}','{1}','{2}')", this.mName, this.description, this.parentId);
				db.ExecuteCommand(sql);
				this.mDatabase = db;
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("UPDATE tbDepartment SET Name = '{0}' ,Description='{1}', ParentId='{2}'  WHERE Id ={3}", new object[]
				{
					this.Name,
					this.description,
					this.parentId,
					base.Id
				});
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override void RemoveFromDb()
		{
			try
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbDepartment WHERE Id = {0}", this.mId);
				this.mDatabase.ExecuteCommand(sql);
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				throw ex;
			}
		}

		public static List<DbDepartment> GetAllDepartments(IDatabase database)
		{
			List<DbDepartment> list = new List<DbDepartment>();
			List<DbDepartment> result;
			try
			{
				if (database.Connection.State == ConnectionState.Closed)
				{
					database.Connection.Open();
				}
				string sql = string.Format("select Id,Name, Description,ParentId from tbDepartment ", Array.Empty<object>());
				DataTable dataTable = database.ExecQuery(sql);
				if (database.Connection.State == ConnectionState.Open)
				{
					database.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int id = Convert.ToInt32(dataTable.Rows[i]["Id"].ToString());
						string name = dataTable.Rows[i]["Name"].ToString();
						string desc = dataTable.Rows[i]["Description"].ToString();
						int num = Convert.ToInt32(dataTable.Rows[i]["ParentId"].ToString());
						list.Add(new DbDepartment(id, name, desc, num, database));
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (database.Connection.State == ConnectionState.Open)
				{
					database.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static DbDepartment GetDepartment(IDatabase database, int id)
		{
			DbDepartment dbDepartment = null;
			DbDepartment result;
			try
			{
				if (database.Connection.State == ConnectionState.Closed)
				{
					database.Connection.Open();
				}
				string sql = string.Format("select Id,Name, Description,ParentId from tbDepartment where id='{0}' ", id);
				DataTable dataTable = database.ExecQuery(sql);
				if (database.Connection.State == ConnectionState.Open)
				{
					database.Connection.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					dbDepartment = new DbDepartment(Convert.ToInt32(dataTable.Rows[0]["Id"].ToString()), dataTable.Rows[0]["Name"].ToString(), dataTable.Rows[0]["Description"].ToString(), Convert.ToInt32(dataTable.Rows[0]["ParentId"].ToString()), database);
				}
				result = dbDepartment;
			}
			catch (Exception ex)
			{
				if (database.Connection.State == ConnectionState.Open)
				{
					database.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static DbDepartment GetDepartment(IDatabase database, string name)
		{
			DbDepartment dbDepartment = null;
			DbDepartment result;
			try
			{
				if (database.Connection.State == ConnectionState.Closed)
				{
					database.Connection.Open();
				}
				string sql = string.Format("select Id,Name, Description,ParentId from tbDepartment where Name='{0}' ", name);
				DataTable dataTable = database.ExecQuery(sql);
				if (database.Connection.State == ConnectionState.Open)
				{
					database.Connection.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					dbDepartment = new DbDepartment(Convert.ToInt32(dataTable.Rows[0]["Id"].ToString()), dataTable.Rows[0]["Name"].ToString(), dataTable.Rows[0]["Description"].ToString(), Convert.ToInt32(dataTable.Rows[0]["ParentId"].ToString()), database);
				}
				result = dbDepartment;
			}
			catch (Exception ex)
			{
				if (database.Connection.State == ConnectionState.Open)
				{
					database.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static void FillDepartmentTree(IDatabase database, TreeView tv)
		{
			try
			{
				tv.Nodes.Clear();
				List<DbDepartment> allDepartments = DbDepartment.GetAllDepartments(database);
				DbDepartment department = DbDepartment.GetDepartment(database, 1);
				TreeNode treeNode = new TreeNode(department.ToString());
				treeNode.Name = department.Name;
				treeNode.Tag = department;
				DbDepartment.SetChildNode(treeNode, allDepartments);
				tv.Nodes.Add(treeNode);
				tv.ExpandAll();
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private static void SetChildNode(TreeNode tn, List<DbDepartment> cl)
		{
			foreach (DbDepartment dbDepartment in cl)
			{
				if (dbDepartment.ParentId == ((DbDepartment)tn.Tag).Id)
				{
					TreeNode treeNode = new TreeNode(dbDepartment.ToString());
					treeNode.Name = dbDepartment.Name;
					treeNode.Tag = dbDepartment;
					treeNode.ImageIndex = 1;
					DbDepartment.SetChildNode(treeNode, cl);
					tn.Nodes.Add(treeNode);
				}
			}
		}

		public override string ToString()
		{
			return this.Name;
		}

		protected string mName = "";

		private string description;

		private int parentId;
	}
}
